/*
 * RentalBO.java
 *
 * Created on March 28, 2007, 7:44 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
 
package edu.byu.isys413.jjenkins.data;

import java.sql.Date;

/**
 * BO for Rental
 * @author Josh
 */
public class RentalBO extends RevenueSourceBO{
    
    private Date dateOut;
    private Date dateDue;
    private ForRentBO forRent = null;
    private String forRentID;
    private TransactionLineBO line;
    
    /** Creates a new instance of RentalBO */
    public RentalBO() {
    }
    
    /**
     * Sets the txLineBO
     * @param line TransactionLineBO
     */
    public void setLine(TransactionLineBO line){
        this.line = line;
        setDirty(true);
    }
    
    /**
     * Gets the TxLineBO
     * @return TransactionLineBO
     */
    public TransactionLineBO getLine(){
        try{
            line = TransactionLineDAO.getInstance().readByRevenueSourceID(this.getId());
        }catch(DataException e){
            e.printStackTrace();
        }
        return line;
    }
    
    /**
     * calculates the price
     * @return double
     */
    public double calcPrice() {
        double pricePerDay = 0;
        double price;
        int numDays = 0;
        try{
            setForRentID(getForRent().getId());
            String conceptualID = ((PhysicalBO)PhysicalDAO.getInstance().read(getForRentID())).getConceptual().getId();
            pricePerDay = ((ConceptualRentalBO)ConceptualRentalDAO.getInstance().read(conceptualID)).getPricePerDay();
        }catch(DataException e){
            e.printStackTrace();
        }
        
        String d1 = getDateOut().toString();
        String d2 = getDateDue().toString();
        int day1 = ((int)d1.charAt(8) - 48) * 10 + ((int)d1.charAt(9) - 48);
        int month1 = ((int)d1.charAt(5) - 48) * 10 + ((int)d1.charAt(6) - 48);
        int day2 = ((int)d2.charAt(8) - 48) * 10 + ((int)d2.charAt(9) - 48);
        int month2 = ((int)d2.charAt(5) - 48) * 10 + ((int)d2.charAt(6) - 48);
        if(month2 > month1){
            for (int i = 0; i < month2 - month1; i++){
                numDays += 31;
            }
            numDays -= day2;
            numDays += day1;
        }else{
            numDays += day2 - day1;
        }
        
        price = (double)numDays * pricePerDay;
        this.setPrice(price);
        return price;
    }
    
    /**
     * Creates a description
     * @return String
     */
    public String createDescription(){
        String desc = null;
        try{
            setForRentID(getForRent().getId());
            desc = ((PhysicalBO)PhysicalDAO.getInstance().read(getForRentID())).getConceptual().getDescription();
        }catch (DataException e){
            e.printStackTrace();
        }
        setDescription(desc);
        return desc;
    }
    
    /**
     * Gets the date rented
     * @return Date
     */
    public Date getDateOut() {
        return dateOut;
    }
    
    /**
     * Sets the date rented
     * @param dateOut Date
     */
    public void setDateOut(Date dateOut) {
        this.dateOut = dateOut;
        setDirty(true);
    }
    
    /**
     * Gets the date due
     * @return Date
     */
    public Date getDateDue() {
        return dateDue;
    }
    
    /**
     * Sets the date due
     * @param dateDue Date
     */
    public void setDateDue(Date dateDue) {
        this.dateDue = dateDue;
        setDirty(true);
    }
    
    /**
     * Gets the forRentBO
     * @throws edu.byu.isys413.jjenkins.data.DataException 
     * @return ForRentBO
     */
    public ForRentBO getForRent() throws DataException{
        if(forRent == null){
            return (ForRentBO)ForRentDAO.getInstance().read(forRentID);
        }
        return forRent;
    }
    
    /**
     * Sets the forRentBO
     * @param forRent ForRentBO
     */
    public void setForRent(ForRentBO forRent) {
        this.forRent = forRent;
        setDirty(true);
    }
    
    /**
     * gets the forRentBO id
     * @return String
     */
    public String getForRentID() {
        return forRentID;
    }
    
    /**
     * Sets the forRentBO id
     * @param forRentID String
     */
    public void setForRentID(String forRentID){
        this.forRentID = forRentID;
        setDirty(true);
    }
    
    /**
     * calculates the days overdue for a rental that the item has not yet been returned.
     * Used in nightly batch processing
     * @throws edu.byu.isys413.jjenkins.data.DataException 
     * @return int
     */
    public int calcDaysOverdueBatch() throws DataException{
        int numDays = 0;
        String d1 = getDateDue().toString();
        String d2 = new Date(System.currentTimeMillis()).toString();
        int day1 = ((int)d1.charAt(8) - 48) * 10 + ((int)d1.charAt(9) - 48);
        int month1 = ((int)d1.charAt(5) - 48) * 10 + ((int)d1.charAt(6) - 48);
        int day2 = ((int)d2.charAt(8) - 48) * 10 + ((int)d2.charAt(9) - 48);
        int month2 = ((int)d2.charAt(5) - 48) * 10 + ((int)d2.charAt(6) - 48);
        if(month2 > month1){
            for (int i = 0; i < month2 - month1; i++){
                numDays += 31;
            }
            numDays -= day2;
            numDays += day1;
        }else{
            numDays += day2 - day1;
        }
        
        return numDays;
    }
    
    /**
     * Returns the number of days late for a returned item
     * @throws edu.byu.isys413.jjenkins.data.DataException 
     * @return int
     */
    public int calcDaysOverdue() throws DataException{
        int numDays = 0;
        RentalReturnBO rentalReturn = RentalReturnDAO.getInstance().getByRentalID(this.getId());
        if(rentalReturn != null){
            String d1 = getDateDue().toString();
            String d2 = rentalReturn.getDateIn().toString();
            int day1 = ((int)d1.charAt(8) - 48) * 10 + ((int)d1.charAt(9) - 48);
            int month1 = ((int)d1.charAt(5) - 48) * 10 + ((int)d1.charAt(6) - 48);
            int day2 = ((int)d2.charAt(8) - 48) * 10 + ((int)d2.charAt(9) - 48);
            int month2 = ((int)d2.charAt(5) - 48) * 10 + ((int)d2.charAt(6) - 48);
            if(month2 > month1){
                for (int i = 0; i < month2 - month1; i++){
                    numDays += 31;
                }
                numDays -= day2;
                numDays += day1;
            }else{
                numDays += day2 - day1;
            }
            
        }
        return numDays;
    }
}
